<template>
  <el-dialog
    :title="!dataForm.sid ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="商品名称" prop="sname">
      <el-input v-model="dataForm.sname" placeholder="商品名称"></el-input>
    </el-form-item>
    <el-form-item label="商品来源" prop="cid">
      <el-input v-model="dataForm.cid" placeholder="商品来源"></el-input>
    </el-form-item>
    <el-form-item label="商品分类" prop="tid">
      <el-input v-model="dataForm.tid" placeholder="商品分类"></el-input>
    </el-form-item>
    <el-form-item label="商品图片" prop="imgs">
      <el-input v-model="dataForm.imgs" placeholder="商品图片"></el-input>
    </el-form-item>
    <el-form-item label="商品价格" prop="price">
      <el-input v-model="dataForm.price" placeholder="商品价格"></el-input>
    </el-form-item>
    <el-form-item label="折扣价格" prop="zprice">
      <el-input v-model="dataForm.zprice" placeholder="折扣价格"></el-input>
    </el-form-item>
    <el-form-item label="是否禁用" prop="ifnot">
      <el-input v-model="dataForm.ifnot" placeholder="是否禁用"></el-input>
    </el-form-item>
    <el-form-item label="商品库存" prop="count">
      <el-input v-model="dataForm.count" placeholder="商品库存"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          sid: 0,
          sname: '',
          cid: '',
          tid: '',
          imgs: '',
          price: '',
          zprice: '',
          ifnot: '',
          count: ''
        },
        dataRule: {
          sname: [
            { required: true, message: '商品名称不能为空', trigger: 'blur' }
          ],
          cid: [
            { required: true, message: '商品来源不能为空', trigger: 'blur' }
          ],
          tid: [
            { required: true, message: '商品分类不能为空', trigger: 'blur' }
          ],
          imgs: [
            { required: true, message: '商品图片不能为空', trigger: 'blur' }
          ],
          price: [
            { required: true, message: '商品价格不能为空', trigger: 'blur' }
          ],
          zprice: [
            { required: true, message: '折扣价格不能为空', trigger: 'blur' }
          ],
          ifnot: [
            { required: true, message: '是否禁用不能为空', trigger: 'blur' }
          ],
          count: [
            { required: true, message: '商品库存不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.sid = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.sid) {
            this.$http({
              url: this.$http.adornUrl(`/user/sou/info/${this.dataForm.sid}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.sname = data.sou.sname
                this.dataForm.cid = data.sou.cid
                this.dataForm.tid = data.sou.tid
                this.dataForm.imgs = data.sou.imgs
                this.dataForm.price = data.sou.price
                this.dataForm.zprice = data.sou.zprice
                this.dataForm.ifnot = data.sou.ifnot
                this.dataForm.count = data.sou.count
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/user/sou/${!this.dataForm.sid ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'sid': this.dataForm.sid || undefined,
                'sname': this.dataForm.sname,
                'cid': this.dataForm.cid,
                'tid': this.dataForm.tid,
                'imgs': this.dataForm.imgs,
                'price': this.dataForm.price,
                'zprice': this.dataForm.zprice,
                'ifnot': this.dataForm.ifnot,
                'count': this.dataForm.count
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
